package org.sbbs.base.web.primefaces;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.primefaces.context.RequestContext;
import org.primefaces.model.UploadedFile;
import org.sbbs.base.dao.BaseDao;

/**
 *
 * @date 2015-08-22 10:04:45
 * @author Mr.FanDangChun
 * @email fdc_messenger@163.com
 */
public abstract class BaseView implements Serializable {

    protected final Logger logger = LogManager.getLogger(this.getClass().getName());
    static protected Properties checcProperties = null;
    protected BaseDao dao;
    protected boolean modify;

    public BaseView() {
        try {
            if (checcProperties == null) {
                InputStream in = getClass().getResourceAsStream("/checc.properties");
                checcProperties = new Properties();
                checcProperties.load(in);
            }
        } catch (IOException ex) {
            logger.error("IOException", ex);
        }
    }

    /**
     * @return the service
     */
    public BaseDao getDao() {
        return dao;
    }

    public boolean isModify() {
        return modify;
    }

    /**
     * @param dao the service to set
     */
    public void setDao(BaseDao dao) {
        this.dao = dao;
    }

    public void setModify(boolean modify) {
        this.modify = modify;
    }

    @PostConstruct
    public abstract void initView();

    public RequestContext getRC() {
        return RequestContext.getCurrentInstance();
    }

    public HttpSession getSession() {
        return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    }

    protected File writeUpdateToLocal(UploadedFile file) throws Exception {
        try {
            String filePath = file.getFileName();
            Properties pps = System.getProperties();
            String separator = pps.getProperty("file.separator");
            String[] sps = filePath.split("\\" + separator);
            String templateDir = BaseView.checcProperties.getProperty("execl_template_up_dir");
            File dir = new File(templateDir);
            if (!dir.exists()) {
                dir.mkdirs();
            } else {
                this.logger.info(dir.getAbsolutePath());
                this.logger.info(dir.getPath());
            }
            String targetPath = dir.getAbsolutePath() + sps[sps.length - 1];
            File uf = new File(targetPath);
            try (final FileOutputStream fop = new FileOutputStream(uf)) {
                fop.write(file.getContents());
                fop.flush();
                fop.close();
                // System.out.println("Done");
            } catch (IOException e) {
                throw e;
            }
            return uf;
        } catch (IOException ex) {
            logger.error(ex);
            throw ex;
        }
    }

    protected void openDialog(String formName) {
        Map<String, Object> options = getDefaultDialogOptions();
        this.getRC().openDialog(formName, options, null);
    }

    protected void openDialog(String formName, int height) {
        Map<String, Object> options = getDefaultDialogOptions();
        options.put("contentHeight", height);
        this.getRC().openDialog(formName, options, null);
    }

    protected void closeDialog(Object obj) {
        this.getRC().closeDialog(obj);
    }

    protected void openDialog(String formName, int width, int height) {
        Map<String, Object> options = getDefaultDialogOptions();
        options.put("contentWidth", width);
        options.put("contentHeight", height);
        this.getRC().openDialog(formName, options, null);
    }

    /**
     *
     * modal false Boolean Controls modality of the dialog. resizable true
     * Boolean When enabled, makes dialog resizable. draggable true Boolean When
     * enabled, makes dialog draggable. width auto String Width of the dialog.
     * height auto String Height of the dialog. contentWidth 640 String Width of
     * the dialog content. contentHeight auto String Height of the dialog
     * content. closable true Boolean Whether the dialog can be closed or not.
     * includeViewParams false Boolean When enabled, includes the view
     * parameters. headerElement null String Client id of the element to display
     * inside header. minimizable false Boolean Makes dialog minimizable.
     * maximizable false Boolean Makes dialog maximizable.
     *
     * @return
     */
    private Map<String, Object> getDefaultDialogOptions() {
        Map<String, Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("draggable", true);
        options.put("resizable", true);
        options.put("contentWidth", 600);
        options.put("contentHeight", 300);
        return options;
    }

    protected void showMessageInDialog(FacesMessage fmsg) {
        this.getRC().showMessageInDialog(fmsg);
    }
}
